﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YK.Component.Data.N
{
    using NH = NHibernate;
    using NHibernate.Linq;
    using YK.Component.Tools;

    /// <summary>
    /// NHibernate 仓储操作基类
    /// </summary>
    public class NHRepositoryBase<TEntity> : IRepository<TEntity> where TEntity : EntityBase
    {
        private NH.ISession _session;
        private bool _useNHibernateManager = true;

        public NHRepositoryBase()
        {

        }

        public NHRepositoryBase(NH.ISession session)
        {
            this._session = session;
            this._useNHibernateManager = false;
        }

        /// <summary>
        /// 
        /// </summary>
        public NH.ISession Session
        {
            get
            {
                if (!_useNHibernateManager)
                {
                    return _session;
                }
                else
                {
                    return NHibernateManager.GetCurrentSession();
                }
            }
        }

        public int Insert(TEntity entity, bool isSave = true)
        {
            int rows = 0;

            Session.Save(entity);
            Session.Flush();

            return rows;
            //throw new NotImplementedException();
        }

        public int Insert(IEnumerable<TEntity> entities, bool isSave = true)
        {
            int rows = 0;

            foreach (var item in entities)
            {
                Session.Save(item);
                Session.Flush();
            }

            return rows;
            //throw new NotImplementedException();
        }

        public int Delete(string id, bool isSave = true)
        {
            int rows = 0;

            var tmp = Session.Get<TEntity>(id);
            if (tmp != null)
            {
                Session.Delete(tmp);
                Session.Flush();
            }
            return rows;
            //throw new NotImplementedException();
        }

        public int Delete(TEntity entity, bool isSave = true)
        {
            int rows = 0;

            Session.Save(entity.Id);
            Session.Flush();

            return rows;
            //throw new NotImplementedException();
        }

        public int Delete(IEnumerable<TEntity> entities, bool isSave = true)
        {
            int rows = 0;
            foreach (var item in entities)
            {
                Session.Save(item);
                Session.Flush();
            }

            return rows;
            //throw new NotImplementedException();
        }

        public int Delete(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate, bool isSave = true)
        {
            //TODO:
            throw new NotImplementedException();
        }

        public int Update(TEntity entity, bool isSave = true)
        {
            int rows = 0;

            Session.Update(entity);
            Session.Flush();

            return rows;
            //throw new NotImplementedException();
        }

        public int Update(System.Linq.Expressions.Expression<Func<TEntity, object>> propertyExpression, TEntity entity, bool isSave = true)
        {
            //TODO:
            int rows = 0;
            //var tmp=session.
            var tmpList = Session.Query<TEntity>().Select(propertyExpression);
            foreach (var itme in tmpList)
            {
                Session.Update(itme);
                Session.Flush();
            }


            return rows;
            //throw new NotImplementedException();
        }

        public TEntity GetByKey(string key)
        {
            return Session.Get<TEntity>(key);

            //throw new NotImplementedException();
        }

        public IQueryable<TEntity> Entities
        {
            get
            {
                return Session.Query<TEntity>();
            }
        }

        /// <summary>
        /// 释放 session
        /// </summary>
        public void Dispose()
        {
            if (this._session != null)
            {
                this._session.Flush();
                CloseSession();
            }
        }


        private void CloseSession()
        {
            _session.Close();
            _session.Dispose();
            _session = null;
        }
    }
}
